package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.coolsk.dao.ProductDao;
import cn.sumpu.app.coolsk.domain.Product;
import cn.sumpu.app.coolsk.domain.ProductPrice;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class ProductDaoImpl extends SqlSessionDaoSupport implements ProductDao {

	private static Logger logger = LoggerFactory.getLogger(ProductDaoImpl.class);

	
	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getProduct(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.ProductMapper.getProduct",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}
	
	

	@Override
	public int getProductCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.ProductMapper.getProductCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public void insertProduct(Product product) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.ProductMapper.insertProduct",
					product);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}

	@Override
	public void updateProduct(Product product) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.ProductMapper.updateProduct",
					product);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}

	@Override
	public void delProduct(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.ProductMapper.delProduct",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}



	@Override
	public void insertProductPrice(ProductPrice productRice)
			throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.ProductMapper.insertProductPrice",
					productRice);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("productRice database failure.", e);
			}
			throw new AppBusinessException("productRice database failure.", e);
		}
		
	}



	@Override
	public void delProductPrice(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.ProductMapper.delProductPrice",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<ProductPrice> getProductPrice(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.ProductMapper.getProductPrice",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getProductPrice database failure.", e);
			}
			throw new AppBusinessException("getProductPrice database failure.", e);
		}
	}

	

	

}
